<?php if (isset($training)):
    ?>

    <?php if ($training->trainingstatus()->name === 'Storniert'): ?>
        <table class="status_table killed">
    <?php else: ?>
            <table class="status_table">
        <?php endif; ?>

            <?php
            $status2 = array(
                'Schwebend',
                'Festgelegt',
                'Geschlossen',
                'Gebucht',
                'Fakturiert',
            );
            $class = '';
            $actual = array_search($training->trainingstatus()->name, $status2);
            for ($i = 0; $i < sizeof($status2); $i++) {
                if ($i < $actual)
                    $class = 'passed';
                elseif ($i == $actual)
                    $class = 'active';
                #if($training->trainingstatus()->name == $status2[$i])  $class = 'active';;
                else
                    $class = '';

                echo '<tr><td class="' . $class . '">' . ($i + 1) . '</td></tr>';
            }
            ?>
        </table>

        <!-- Training details START HERE -->
        <table class="table table-condensed table-striped" style="width: 90%;">
            <!-- Course related information -->
            <tr>
                <th>Thema</th>
                <td><?php echo $training->traininggroup()->course()->title; ?> <?php echo '(' . $training->traininggroup()->course()->faculty()->title . ')'; ?>
                </td>
            </tr>
            <!-- Cost related information -->
            <tr>
                <th>Kosten</th>
                <td><?php
        echo anchor('training/details/' . $training->id . '?edit=cost', ' ', array('class' => 'ic-coins_add', 'style' => 'float:left;'));
        // check for existing costs
        if ($training->traininggroup()->cost_exists()) {
            $costs = $training->traininggroup()->cost();
            $costs_complete = 0;                                        #sum of all costs
            echo '<ul style="list-style:none;">';                                                                    # @todo: style ul?
            foreach ($costs as $cost) {
                echo '<li>' . number_format($cost->amount, 2) .
                ' €: ' . $cost->costtype()->name .
                '</li>';
                $costs_complete = $costs_complete + $cost->amount;      #straight forward addition yay!
            }
        }

        else
        // no costs present
            echo '<li>n/a</li>';

        // check if user wants to add a cost
        if (isset($edit) AND $edit == 'cost') {
            echo '<li>';
            echo form_open('/training/add_cost/' . $training->id, '');
            echo form_input('cost-amount', '0,00');
            echo form_dropdown('costtype', $costtypes, '');
            echo form_submit('add_cost', 'OK');
            echo form_close();
            echo '</li>';
            echo '<hr style="margin:2px 0;">';
        }
        if (isset($costs_complete)) {
            echo '<li>Gesamt: ' . number_format($costs_complete, 2) . ' €';
        }
        echo '</ul>';
            ?>
                </td>
            </tr>

            <!-- Attendee related information -->
            <tr>
                <th>Teilnehmer</th>
                <td><?php
                echo count($training->employee());
                // if max_participants is set, show it. Else show some dashes...
                echo ($training->traininggroup()->max_participants) ? '/' . $training->traininggroup()->max_participants : '/--';
            ?>
                </td>
            </tr>

            <!-- Location related information -->
            <?php if ($training->place_exists()): ?>
                <tr>
                    <th>Veranstalltungsort</th>
                    <td><?php
                $location = $training->place();
                if (isset($location->street))
                    echo $location->street . ' ' . $location->streetnr . '<br>';
                echo $location->postalcode . ' ' . $location->city;
                ?>
                    </td>
                </tr>

                <tr>
                    <th>Raum</th>
                    <td><?php echo $location->room; ?> <a href="" class="ic-house"></a>
                    </td>
                </tr>

                <!-- Trainer information -->
            <?php endif; ?>
            <?php if ($training->traininggroup()->trainer_exists()): ?>
                <tr>
                    <th>Trainer</th>
                    <td><?php
        $contactdata = $training->traininggroup()->trainer();
        echo $contactdata->forename . ', ' . $contactdata->surname;
                ?>
                    </td>
                </tr>

                <!-- Status information -->
            <?php endif; ?>
            <tr>
                <th>Status</th>
                <td>
                    <div rel="tooltip"
                         title="<?php echo $training->trainingstatus()->description; ?>"
                         class="statusbox <?php echo $training->trainingstatus()->name; ?>">
                             <?php echo $training->trainingstatus()->name; ?>
                    </div>&nbsp;
                </td>
            </tr>
            
            
            <!-- Display this Part only in the right status -->
            <?php if($training->trainingstatus()->name == 'Gebucht' || $training->trainingstatus()->name == 'Fakturiert'): ?>
            <tr>
                <th>Dokumente</th>
                <td>
                    <?php if(file_exists('./uploads/'.$training->offer()->path)): ?>
                        <span>Angebot:</span>
                            <?php $fullname_offer = $training->offer()->path; 
                                  $filename_offer  = substr($fullname_offer, strpos($fullname_offer, '/')+1);
                                  $mimeType_offer = substr($fullname_offer, strpos($fullname_offer, '.')+1);
                            ?>
                            <a href="<?php echo base_url('uploads/'.$fullname_offer); ?>" style="font-size:14px">
                            <?php echo img('img/mime/'.strtolower($mimeType_offer).'-icon.png'); ?>
                            <?php echo $filename_offer ?>
                        </a>
                        <span>(Einkaufwagennummer:<?php echo $training->cart_number; ?>)</span>
                    <?php else: ?>
                        <div class="alert alert-danger" style="font-size:13px; line-height: 13px; padding:7px; max-width: 400px"><?php echo img(array('src' => 'img/icons/exclamation.png', 'style' => 'display:block; margin:12px 10px 12px 3px; float:left;')); ?>Achtung: Die Angehängten Datei(en) exisiteren im Dateisystem nichtmehr!<br > Ggf wurde diese gelöscht oder verschoben.<br > Bitte benachrichtigen Sie den Administrator!</div>
                    <?php endif; ?>
                    <?php if($training->trainingstatus()->name == 'Fakturiert'): ?>
                        <br >  
                        <?php if(file_exists('./uploads/'.$training->bill()->path)): ?>
                            <span>Rechnung:</span>
                                <?php $fullname_bill = $training->bill()->path; 
                                      $filename_bill  = substr($fullname_bill, strpos($fullname_bill, '/')+1);
                                      $mimeType_bill = substr($fullname_bill, strpos($fullname_bill, '.')+1);
                                ?>
                                <a href="<?php echo base_url('uploads/'.$fullname_bill); ?>" style="font-size:14px">
                                <?php echo img('img/mime/'.strtolower($mimeType_bill).'-icon.png'); ?>
                                <?php echo $filename_bill ?>
                            </a>
                        <?php else: ?>
                            <br ><div class="alert alert-danger" style="font-size:13px; line-height: 13px; padding:7px; max-width: 400px"><?php echo img(array('src' => 'img/icons/exclamation.png', 'style' => 'display:block; margin:12px 10px 12px 3px; float:left;')); ?>Achtung: Die Angehängten Datei(en) exisiteren im Dateisystem nichtmehr!<br > Ggf wurde diese gelöscht oder verschoben.<br > Bitte benachrichtigen Sie den Administrator!</div>
                        <?php endif; ?>
                    <?php endif; ?>
                        
                </td>
            </tr>
            <?php endif; ?>
        </table>

    <?php else: ?>
        <div class="alert alert-danger">Keine Maßnahme gefunden!</div>
    <?php endif; ?>
    <br style="clear: both">
